//package com.lengyu.iottransfer.gateway.config;
//
//import com.alibaba.fastjson.JSONObject;
//import com.lengyu.iottransfer.gateway.common.ByteUtils;
//import com.lengyu.iottransfer.gateway.service.impl.ElecLcdTransferService;
//import com.lengyu.iottransfer.gateway.service.impl.ZhengqiF2001XTransferService;
//import com.lengyu.iottransfer.gateway.tcp.daxiang.DaxiangGuanpianTransfer;
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.integration.annotation.ServiceActivator;
//import org.springframework.integration.channel.DirectChannel;
//import org.springframework.integration.core.MessageProducer;
//import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
//import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
//import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
//import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
//import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageChannel;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessagingException;
//
//import java.util.Arrays;
//
//@Configuration
//public class MqttConfigForRecieveString {
//
//    private final static String clientId = "iot_transfer_call_service";
//
//    private final static Logger logger = LoggerFactory.getLogger(MqttConfigForRecieveString.class);
//
////    private final  static String  mqtt_addr= "tcp://58.241.13.10:1883";
//    private final  static String  mqtt_addr= "tcp://172.2.0.28:1883";
//
//    @Autowired
//    DaxiangGuanpianTransfer daxiangGuanpianTransfer;
//
//    /**
//     * 连接mqtt服务器的工厂
//     * @return
//     */
//    @Bean
//    public MqttPahoClientFactory mqttClientFactory() {
//        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
//        MqttConnectOptions options = new MqttConnectOptions();
//        options.setServerURIs(new String[] { mqtt_addr });
//        options.setUserName("iot_transfer");
//        options.setPassword("tjgk_xinxishi".toCharArray());
//        factory.setConnectionOptions(options);
//        return factory;
//    }
//
//    @Bean
//    public MessageChannel mqttOutboundChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    @ServiceActivator(inputChannel = "mqttOutboundChannel") //inputChannel的名字必须和上面的MessageChannel的方法名保持一致
//    public MessageHandler mqttOutbound() {
//        MqttPahoMessageHandler messageHandler =
//                new MqttPahoMessageHandler(clientId + "_outbound" + System.currentTimeMillis(), mqttClientFactory());
//        messageHandler.setAsync(true);
//        return messageHandler;
//    }
//
//    /**
//     * 收消息的通道，注意实际开发中和发的可能不在一起
//     * @return
//     */
//    @Bean
//    public MessageChannel mqttInputChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    public MessageProducer inbound() {
////        // 设置转换器，接收bytes
////        DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
////        converter.setPayloadAsBytes(true);
//
//        MqttPahoMessageDrivenChannelAdapter adapter =
//                new MqttPahoMessageDrivenChannelAdapter(mqtt_addr, clientId+ "_inbound" + System.currentTimeMillis(),
//                        "/lengyu_iot/service/call/#");
//        adapter.setCompletionTimeout(5000);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
////        adapter.setConverter(converter);
//        adapter.setOutputChannel(mqttInputChannel());
//        return adapter;
//    }
//
//    /**
//     * 收消息的处理器，用于如何处理消息
//     * mqttInputChannel 代表的是收消息的通道对象的id
//     * @return
//     */
//    @Bean
//    @ServiceActivator(inputChannel = "mqttInputChannel")
//    public MessageHandler handler() {
//        return new MessageHandler() {
//            @Override
//            public void handleMessage(Message<?> message) throws MessagingException {
//                try{
//
//                    String topic = message.getHeaders().get("mqtt_receivedTopic").toString();
//
//                    if(topic.startsWith("/lengyu_iot/service/call/")){
//                        if(topic.endsWith("1f421904c7e2e08f56b890af282c3a20")){
//                            logger.info("预埋传感器下行命令a：" + message.getPayload());
//                            daxiangGuanpianTransfer.sendCommandToDevice(JSONObject.parseObject(message.getPayload().toString()));
//                        }
//                    }
//                }
//                catch(Exception ex){
//                    logger.error("err",ex);
//                }
//            }
//        };
//    }
//
//}
